<template>
    <div class="page-wrap">
        <div class="top-people" @click="navTo('/subAgent')">
            <img class="head" src="@/assets/agentImg.png" mode="widthFix" />
            <div class="c">{{ agent.AGENT_NAME ? agent.AGENT_NAME : '选择服务商' }}</div>
            <img class="right" src="@/assets/chevron-down@2x.png" mode="widthFix" />
        </div>
        <div class="bottom">
            转让积分
            <div class="my-input">
                <div class="in">
                    <van-field v-model="AMultiple" type="number" placeholder="请输入转让积分" />
                </div>
            </div>
            <div class="prompt">
                <text>积分转让最小为：1000</text>
                <text class="all" @click="all">全部</text>
            </div>
            <div class="btn" @click="transfer">确定</div>
        </div>
        <van-popup v-model:show="showKeyboard1" round :style="{ width: '80%' }">
            <div class="txInfo">
                <div class="title">积分转让</div>
                <div class="t1">
                    积分：
                    <span>{{ AMultiple }}</span>
                </div>
                <van-divider />
                <div class="fl">
                    <div class="top">
                        <span>转让给</span>
                        <span>{{ agent.AGENT_NAME }}</span>
                    </div>
                </div>
                <van-password-input
                    :gutter="10"
                    :safe-area-inset-bottom="true"
                    :value="value"
                    :focused="(showKeyboard = false)"
                    @focus="showKeyboard = true"
                />
            </div>
        </van-popup>
        <div>
            <!-- 数字键盘 -->
            <van-number-keyboard
                z-index="2024"
                random-key-order
                :hide-on-click-outside="false"
                v-model="valueKeyboard"
                :show="showKeyboard"
                @blur="showKeyboard = false"
                @input="onInput"
                @delete="onDelete"
            />
        </div>
    </div>
</template>

<script>
import { downRegulationPoints } from '@/api/agent'
import JSEncrypt from 'jsencrypt'
import { payPwdExist } from '@/api/wallet'
import { showDialog, showConfirmDialog, showToast } from 'vant'
import store from '../../store'

export default {
    name: 'transferDown',
    data() {
        return {
            valueKeyboard: '',
            value: '',
            showKeyboard1: false,
            showKeyboard: false,
            balance: '',
            AMultiple: '', //转让倍数
            agent: {}
        }
    },
    created(option) {
        this.balance = this.$route.query.balance
        this.agent = store.state.data.agentInfo
    },
    methods: {
        navTo(url) {
            this.$router.push({
                path: url
            })
        },
        all() {
            if (this.balance < 1000) {
                showToast('积分不足1000,无法转让')

                return
            } else {
                this.AMultiple = this.balance
            }
        },
        transfer() {
            if (!this.agent.AGENT_NO) {
                showToast('请选择下级服务商')
                return
            }

            if (!this.AMultiple) {
                showToast('请输入转让积分数量')

                return
            }
            if (Number(this.AMultiple) < 1000) {
                showToast('积分转让最小为：1000')
                return
            }
            // alert(Number(this.AMultiple),)
            if (Number(this.AMultiple) > Number(this.balance)) {
                showToast('积分不足')
                return
            }
            payPwdExist().then(res => {
                if (res.object.isExist === 'NO') {
                    // 未设置支付密码
                    showConfirmDialog({
                        message: '你暂未重置支付密码，请重置密码',
                        title: '支付密码',
                        confirmButtonColor: '#66d6ff'
                    }).then(() => {
                        this.$router.push({
                            path: '/getCode',
                            query: {
                                pay: 1
                            }
                        })
                        // uni.navigateTo({
                        //     url: '../payPwd/getCode?pay=1'
                        // })
                    })
                } else {
                    showConfirmDialog({
                        message: `确认转让${this.AMultiple}积分至${this.agent.AGENT_NAME}吗？`,
                        title: '积分转让',
                        confirmButtonColor: '#66d6ff'
                    })
                        .then(() => {
                            this.showKeyboard1 = true
                            setTimeout(() => {
                                this.showKeyboard = true
                            }, 600)
                        })

                        .catch(() => {
                            // on cancel
                        })
                }
            })
        },
        onInput(value) {
            console.log(value)
            console.log(value.toString().length)
            this.value = this.value + value.toString()
            if (this.value.length == 6) {
                const key =
                    'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAicjwP+F30h1RmMZuAjKIW7+pJxK6NlhvsNks28X4/91iF7lBoZd57ZuN+310vaFxol8khFd5SDbnx7piqPxuAaUC/IygD4iPn2ZSSA5pqqdQYodJVckyh4PNjbh6lDkjYNoAGhz1BY+AiQz2uPTNFnOqmDu1ZfhoQCWMhSleW0zikybl3KRcczDja96+DmKkssXAYN5clnnZMcBjuWOwBz5pgHiQEQTatdWm/un3SR366LrnKAXYUyAEUxwuEA9qtiaDvKgLzSl84FqmvU6cLzmLJM+b+++0CONoT0IR9I6XFkF/wErnd8Y+gEB4oQEURe39BUjl5VcxSJxf1HorSQIDAQAB'
                // 调注册接口
                let jse = new JSEncrypt()
                jse.setPublicKey(key)
                let payPwd = jse.encrypt(this.value)
                downRegulationPoints({
                    amount: this.AMultiple,
                    targetNo: this.agent.AGENT_NO,
                    payPwd
                }).then(res => {
                    if (res.code == 200) {
                        this.showKeyboard = false
                        showDialog({
                            title: '转让结果',
                            message: res.message || '转让成功',
                            showCancelButton: false
                        }).then(() => {
                            this.$router.go(-1)
                        })
                    } else {
                        showToast(res.message)
                        this.value = ''
                        this.valueKeyboard = ''
                    }
                })
            }
        },
        onDelete(value) {
            console.log(this.value)
            this.value = this.value.slice(0, this.value.length - 1)
        },

        upload(data) {
            let password = data.password
            // 环迅
            const key =
                'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAicjwP+F30h1RmMZuAjKIW7+pJxK6NlhvsNks28X4/91iF7lBoZd57ZuN+310vaFxol8khFd5SDbnx7piqPxuAaUC/IygD4iPn2ZSSA5pqqdQYodJVckyh4PNjbh6lDkjYNoAGhz1BY+AiQz2uPTNFnOqmDu1ZfhoQCWMhSleW0zikybl3KRcczDja96+DmKkssXAYN5clnnZMcBjuWOwBz5pgHiQEQTatdWm/un3SR366LrnKAXYUyAEUxwuEA9qtiaDvKgLzSl84FqmvU6cLzmLJM+b+++0CONoT0IR9I6XFkF/wErnd8Y+gEB4oQEURe39BUjl5VcxSJxf1HorSQIDAQAB'
            let jse = new JSEncrypt()
            jse.setPublicKey(key)
            let payPwd = jse.encrypt(password)
            downRegulationPoints({
                amount: this.AMultiple,
                targetNo: this.agent.AGENT_NO,
                payPwd
            }).then(res => {
                if (res.code === 200) {
                    this.showKeyboard = false
                    showDialog({
                        title: '转让结果',
                        message: res.message || '转让成功',
                        showCancelButton: false
                    }).then(() => {
                        this.$router.go(-1)
                    })
                }
            })
        }
    }
}
</script>

<style scoped lang="scss">
.page-wrap {
    padding: 10px;
    .top-people {
        display: flex;
        align-items: center;
        font-size: 16px;
        padding-right: 10px;
        .head {
            margin-right: 15px;
            width: 40px;
        }
        .c {
            flex: 1;
        }
        .right {
            width: 15px;
        }
    }
    .bottom {
        padding: 12px;
        box-sizing: border-box;
        margin-top: 15px;
        line-height: 35px;
        background: #ffffff;
        opacity: 1;
        border-radius: 6px;
        .my-input {
            padding: 10px 0;
            .in {
                width: 80%;
                transform: scale(1.2) translate(10%);
                border-bottom: 1px solid #ccc;
                .uni-easyinput__content-input {
                    height: 50px;
                }
            }
            display: flex;
            align-items: center;
        }
        .prompt {
            .all {
                margin-left: 25px;
                color: #66d6ff;
            }
        }
        .btn {
            margin: 20px auto;
            width: 152px;
            height: 40px;
            background: #66d6ff;
            opacity: 1;
            border-radius: 10px;
            font-size: 14px;
            font-weight: 400;
            color: #ffffff;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    }
    .txInfo {
        padding: 10px 20px;
        box-sizing: border-box;
        .title {
            font-weight: 600;
            text-align: center;
        }
        .t1 {
            text-align: center;
            margin-top: 30px;
            span {
                font-size: 30px;
                font-weight: 600;
            }
        }
        .fl {
            line-height: 40px;
            color: #545454;
            margin-bottom: 20px;
            .top {
                display: flex;
                justify-content: space-between;
            }
            .bottom {
                display: flex;
                justify-content: space-between;
            }
        }
    }
}
</style>
<style>
.van-password-input {
    margin: 0;
}
.van-password-input__security li {
    background-color: #eee;
    border-radius: 5px;
    height: 40px;
}
</style>
